import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * \* User: MeiZhongHao
 * \* Date: 2019-04-22
 * \* Time: 16:28
 * \* Description:
 * \
 */
public class BigFileHandler {

    public static void mappedBuffer(String from, String to) throws IOException {
        long start = System.currentTimeMillis();
        FileChannel read = new FileInputStream(from).getChannel();
        FileChannel writer = new RandomAccessFile(to, "rw").getChannel();
        long i = 0;
        long size = read.size() / 30;
        ByteBuffer bb, cc = null;
        while (i < read.size() && (read.size() - i) > size) {
            bb = read.map(FileChannel.MapMode.READ_ONLY, i, size);
            cc = writer.map(FileChannel.MapMode.READ_WRITE, i, size);
            cc.put(bb);
            i += size;
            bb.clear();
            cc.clear();
        }
        bb = read.map(FileChannel.MapMode.READ_ONLY, i, read.size() - i);
        cc.put(bb);
        bb.clear();
        cc.clear();
        read.close();
        writer.close();
        long end = System.currentTimeMillis();
        System.out.println("用时：" + (end - start) + "毫秒");
    }
}